using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using System.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;

namespace Dropthings.DAL
{
    public class BaseProcedure
    {
        #region Member fields
        private static string DbConnectionConfigName = "DropthingsConnectionString";

        private Database _GlobalDB;
        private DbConnection _GlobalConnection;
        private DbTransaction _GlobalTransaction;

        protected string _StoredProcedureName = "";
        protected SqlParameter[] _Parameters;
        #endregion

        #region Properties
        private Database GlobalDB
        {
            get { return _GlobalDB; }
            set { _GlobalDB = value; }
        }

        private DbConnection GlobalConnection
        {
            get { return _GlobalConnection; }
            set { _GlobalConnection = value; }
        }

        private DbTransaction GlobalTransaction
        {
            get { return _GlobalTransaction; }
            set { _GlobalTransaction = value; }
        }

        protected string StoredProcedureName
        {
            get { return _StoredProcedureName; }
            set { _StoredProcedureName = value; }
        }

        protected SqlParameter[] Parameters
        {
            get { return _Parameters; }
            set { _Parameters = value; }
        }
        #endregion

        #region Constructor
        protected BaseProcedure()
        {
            this._GlobalDB = null;
            this._GlobalConnection = null;
            this._GlobalTransaction = null;
        }

        protected BaseProcedure(Database db, DbConnection conn, DbTransaction tran)
        {
            this.GlobalDB = db;
            this.GlobalConnection = conn;
            this.GlobalTransaction = tran;
        }
        #endregion

        #region Static Methods
        public static Database CreateDatabase()
        {
            return DatabaseFactory.CreateDatabase(DbConnectionConfigName);
        }
        #endregion

        #region Public Methods
        public int ExecuteNonQuery()
        {
            Database db = GlobalDB;
            if (db == null)
            {
                db = DatabaseFactory.CreateDatabase(DbConnectionConfigName);
            }

            DbCommand dbCommand = db.GetStoredProcCommand(StoredProcedureName);
            if (Parameters != null)
            {
                dbCommand.Parameters.AddRange(Parameters);
            }

            if (GlobalTransaction == null)
            {
                return db.ExecuteNonQuery(dbCommand);
            }
            else
            {
                return db.ExecuteNonQuery(dbCommand, GlobalTransaction);
            }

        }

        public DataSet ExecuteDataset()
        {
            Database db = GlobalDB;
            if (db == null)
            {
                db = DatabaseFactory.CreateDatabase(DbConnectionConfigName);
            }

            DbCommand dbCommand = db.GetStoredProcCommand(StoredProcedureName);
            if (Parameters != null)
            {
                dbCommand.Parameters.AddRange(Parameters);
            }

            if (GlobalTransaction == null)
            {
                return db.ExecuteDataSet(dbCommand);
            }
            else
            {
                return db.ExecuteDataSet(dbCommand, GlobalTransaction);
            }
        }

        public object ExecuteScalar()
        {
            Database db = GlobalDB;
            if (db == null)
            {
                db = DatabaseFactory.CreateDatabase(DbConnectionConfigName);
            }

            DbCommand dbCommand = db.GetStoredProcCommand(StoredProcedureName);
            if (Parameters != null)
            {
                dbCommand.Parameters.AddRange(Parameters);
            }

            if (GlobalTransaction == null)
            {
                return db.ExecuteScalar(dbCommand);
            }
            else
            {
                return db.ExecuteDataSet(dbCommand, GlobalTransaction);
            }
        }
        #endregion
    }
}
